const path = require('path')
const express = require('express')
const morgan = require('morgan')
const rfs = require('rotating-file-stream')
require('dotenv').config()
require('./db')
require('./model')
require('./common/r')
require('./middleware')
const app = express()
const bodyParser = require('body-parser')
const cors = require('cors')
const accessLogStream = rfs.createStream('access.log', {
  interval: '1d', // rotate daily
  path: path.join(__dirname, 'log')
})
app.use(morgan('combined', { stream: accessLogStream }))

// 允许传递json数据
app.use(bodyParser.json())
// 允许跨域
app.use(cors())
const router = express.Router()
require('./routes')(router, app)
const port = process.env.PORT || 3000

app.listen(port, (err) => {
  if (err) {
    console.log(`项目运行失败：${err}`)
    return
  }
  console.log(`app listening at http://localhost:${port}`)
})